package org.gongliang.jsmile.authc.sys.controller;


import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.gongliang.jsmile.common.constants.FileConstant;
import org.gongliang.jsmile.common.kit.file.FileKit;
import org.gongliang.jsmile.common.kit.file.UploadFile;
import org.gongliang.jsmile.config.oss.OSSFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baidu.ueditor.ActionEnter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import springfox.documentation.annotations.ApiIgnore;

@Controller
@ApiIgnore
public class UeditorController {

	/**
	 * desc: uedit后端配置
	 */
	@RequestMapping("/ueditor/config")
	@ResponseBody
	public JSONObject ueditor(HttpServletRequest request) {
		String rootPath = request.getSession().getServletContext().getRealPath("/");
		String result = new ActionEnter(request, rootPath).exec();
		return JSON.parseObject(result);
	}

	/**
	 * desc: uedit图片上传
	 */
	@RequestMapping("/ueditor/uploadImage")
	@ResponseBody
	public Map<String, Object> uploadImage(HttpServletRequest request) {
		return upload(FileConstant.ueditor_images, request);
	}

	/**
	 * desc: uedit附件上传
	 */
	@RequestMapping("/ueditor/uploadFile")
	@ResponseBody
	public Map<String, Object> uploadFile(HttpServletRequest request) {
		return upload(FileConstant.ueditor_files, request);
	}

	/**
	 * desc: uedit视频上传
	 */
	@RequestMapping("/ueditor/uploadVideo")
	@ResponseBody
	public Map<String, Object> uploadVideo(HttpServletRequest request) {
		return upload(FileConstant.ueditor_videos, request);
	}

	/**
	 * desc: uedit附件列表
	 */
	@RequestMapping("/ueditor/listFile")
	@ResponseBody
	public Map<String, Object> listFile(HttpServletRequest request) {
		return uploadList(FileConstant.ueditor_files, request);
	}

	/**
	 * desc: uedit图片列表
	 */
	@RequestMapping("/ueditor/listImage")
	@ResponseBody
	public Map<String, Object> listImage(HttpServletRequest request) {
		return uploadList(FileConstant.ueditor_images, request);
	}

	private Map<String, Object> uploadList(String filename, HttpServletRequest request) {
		Map<String, Object> m = Maps.newHashMap();
		m.put("state", "SUCCESS");
		m.put("total", request.getParameter("size"));
		m.put("start", request.getParameter("start"));

		List<Map<String, Object>> files = Lists.newArrayList();

		List<String> urls = OSSFactory.build().listFiles(filename);
		for (String tem : urls) {
			Map<String, Object> url = Maps.newHashMap();
			url.put("state", "SUCCESS");
			url.put("url", tem);
			files.add(url);
		}
		m.put("list", files);
		return m;
	}

	/**
	 * desc: 文件上传
	 */
	private Map<String, Object> upload(String filename, HttpServletRequest request) {
		Map<String, Object> m = Maps.newHashMap();
		List<UploadFile> files = FileKit.multiUpload(request, filename);
		m.put("state", "SUCCESS");
		if (files.size() > 0) {
			m.put("url", files.get(0).getUrl());
			m.put("title", files.get(0).getOriginalFilename());
		}
		m.put("original", request.getParameter("name"));
		m.put("type", request.getParameter("type"));
		m.put("size", request.getParameter("size"));
		return m;
	}
}
